import React from 'react';
import { access } from 'components/accessContainer/util';
import { ORGANIZATION_ADMIN_ROLES } from 'utils/constants';
import { isAliyunDomain } from 'utils/tenant';
import intl from '../../../../../locale';

const { user_role } = window.FORCE.organization;
export default class TooLargeTip extends React.Component {
  componentWillUnmount() {
    window.TB_NAVIGATION.Announcement.unregister();
  }

  getProjectInfoFromPageInfo = () => {
    const { pathResource } = this.props.pageInfo || {};
    return {
      role: pathResource.role,
      repoSize: Number(pathResource.repoSize),
      // 默认2G
      repoLimitSize: pathResource.repoLimitSize ? Number(pathResource.repoLimitSize) : 2048,
      renewal_flag: pathResource.renewal_flag,
      plan_type: pathResource.plan_type,
      history_scale_repo: pathResource.history_scale_repo,
    };
  };

  generateInnerHtml = (commonText, helpLink, linkText) => {
    return (
      <span>
        {commonText}
        <a href={helpLink} target="_blank" rel="noreferrer">
          {linkText}
        </a>
      </span>
    );
  };

  configAdminAndMemberText = (planType, historyScaleRepo) => {
    let adminCommon;
    let memberCommon;
    let adminHelpLink;
    let memberHelpLink;
    let adminLinkText;
    let memberLinkText;
    const { organization_id } = window.FORCE.organization;

    // 领取福利包、续费
    const cost_url = `https://devops.aliyun.com/org-admin/${organization_id}/profile?buy=true&step=resource`;
    // const free_url = `https://devops.aliyun.com/org-admin/${organization_id}/profile?buy=true`;

    // 兜底文案
    adminCommon =
      '代码库容量超过推荐阈值，无法提交，请清理仓库文件，推荐使用Git LFS管理二进制大文件，释放Git存储空间。 ';
    memberCommon =
      '容量超过推荐阈值，无法提交，请联系管理员清理文件，推荐使用Git LFS管理二进制大文件，释放Git存储空间。 ';

    adminHelpLink = 'https://help.aliyun.com/document_detail/153791.html';
    memberHelpLink = 'https://help.aliyun.com/document_detail/153791.html';

    adminLinkText = '了解更多';
    memberLinkText = '了解更多';

    if (planType !== 'premiumVersion' && historyScaleRepo) {
      adminCommon =
        '代码库容量超过推荐阈值，无法提交，请清理仓库文件，推荐使用Git LFS管理二进制大文件，释放Git存储空间。 ';
      memberCommon =
        '容量超过推荐阈值，无法提交，请联系管理员清理文件，推荐使用Git LFS管理二进制大文件，释放Git存储空间。 ';
    } else if (planType !== 'premiumVersion' && !historyScaleRepo) {
      adminCommon = '代码库容量超过推荐阈值，无法提交，立即升级套餐获取更多容量。 ';
      memberCommon = '容量超过推荐阈值，无法提交，请联系企业管理员升级套餐。 ';

      adminHelpLink = cost_url;
      memberHelpLink = 'https://help.aliyun.com/document_detail/173288.html';

      adminLinkText = '升级套餐';
      memberLinkText = '了解更多';
    }
    const adminText = this.generateInnerHtml(adminCommon, adminHelpLink, adminLinkText);
    const memberText = this.generateInnerHtml(memberCommon, memberHelpLink, memberLinkText);

    return {
      adminText,
      memberText,
    };
  };

  render() {
    const { repoSize, repoLimitSize, renewal_flag, plan_type, history_scale_repo } =
      this.getProjectInfoFromPageInfo();
    const isTooLarge = repoSize >= repoLimitSize;

    const { adminText, memberText } = this.configAdminAndMemberText(
      plan_type,
      history_scale_repo,
      renewal_flag,
    );

    if (isTooLarge) {
      window.TB_NAVIGATION.Announcement.register({
        componentType: 'banner',
        title: access(ORGANIZATION_ADMIN_ROLES, user_role) ? (
          <div>
            {isAliyunDomain
              ? adminText
              : intl.get({
                  id: 'code-assets.containers.projectTab.tooLargeTip.TheDataInTheCode',
                  defaultMessage:
                    '代码库数据超过容量上限，后续无法进行代码提交，请前往「设置-存储空间管理」优化。',
                })}
          </div>
        ) : (
          <div>
            {isAliyunDomain
              ? memberText
              : intl.get({
                  id: 'code-assets.containers.projectTab.tooLargeTip.TheDataInTheCode.1',
                  defaultMessage:
                    '代码库数据超过容量上限，后续无法进行代码提交，请联系管理员前往「设置-存储空间管理」优化。',
                })}
          </div>
        ),
        type: 'error',
        closeable: false,
      });
    }

    return <></>;
  }
}
